package com.ywgt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.ywgt.express.ExpressService;
import com.ywgt.pojo.Comment;
import com.ywgt.pojo.Order;
import com.ywgt.pojo.OrderGoods;
import com.ywgt.pojo.User;
import com.ywgt.service.CommentService;
import com.ywgt.service.OrderGoodsService;
import com.ywgt.service.OrderService;
import com.ywgt.service.UserService;
import com.ywgt.util.ResponseUtil;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin/order")
@ApiModel("订单管理")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderGoodsService orderGoodsService;

    @Autowired
    UserService userService;

    @Autowired
    ExpressService expressService;

    @Autowired
    CommentService commentService;
    /**
     * page: 1,
     * limit: 20,
     * id: undefined,
     * name: undefined,
     * orderStatusArray: [],
     * sort: 'add_time',
     * order: 'desc'
     * @return
     */

    @GetMapping("/listShipChannel")
    public Object getShipChannel(){
        Map<String,Object> data=new HashMap<>();
        List<Map<String, String>> allVendor = expressService.getAllVendor();
        data.put("shipChannelList",allVendor);
        return ResponseUtil.ok(data);
    }
    @GetMapping("/list")
    public Object getOrders(Integer[] orderStatusArray, Integer userId, String orderSn,@RequestParam(required = false,defaultValue = "add_time") String sort,
                            @RequestParam(required = false,defaultValue = "desc") String order,
                            @RequestParam(required = false,defaultValue = "1") int page,
                            @RequestParam(required = false,defaultValue = "10") int limit){
        Page<Order> orders=orderService.getOrders(orderStatusArray,userId,orderSn,page,limit,order,sort);
        List<Order> items = orders.getResult().stream().filter(o->!o.getDeleted()).collect(Collectors.toList());
        long total = orders.getTotal();
        Map<String,Object> data=new HashMap<>();
        List<Map<String, String>> allVendor = expressService.getAllVendor();
        List<Map<String, Object>> shipChannelList = new ArrayList<Map<String, Object>>(allVendor == null ? 0 : allVendor.size());
        for (Map<String, String> map : allVendor) {
            Map<String, Object> b = new HashMap<>(2);
            b.put("value", map.get("code"));
            b.put("label", map.get("name"));
            shipChannelList.add(b);
        }
        data.put("shipChannelList",shipChannelList);
        data.put("total",total);
        data.put("items",items);
        return ResponseUtil.ok(data);
    }


    @GetMapping("/detail")
    public Object detail(Integer id){
        Order order = orderService.getById(id);
        LambdaQueryWrapper<OrderGoods> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(OrderGoods::getId,order.getId());
        List<OrderGoods> orderGoods = orderGoodsService.list(wrapper);
        User user = userService.getById(order.getUserId());
        Map<String,Object> data=new HashMap<>();
        data.put("user",user);
        data.put("orderGoods",orderGoods);
        data.put("order",order);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/reply")
    public Object reply(@RequestBody Map<String,Object> requestMap){
        if(commentService.reply(requestMap)){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }
}
